home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / vg-2.03 / jpeg / jdscan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  9.3 KB  |  386 lines

  1. /*
  2.  * Copyright (C) 1992-1993 Michael Davidson.
  3.  * All rights reserved.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software
  6.  * and its documentation for any purpose and without fee is hereby
  7.  * granted, provided that the above copyright notice appear in all
  8.  * copies and that both that copyright notice and this permission
  9.  * notice appear in supporting documentation.
  10.  *
  11.  * This software is provided "as is" without express or implied warranty.
  12.  */
  13.  
  14. /*
  15.  * This software is derived from the Independent JPEG Group's software
  16.  *
  17.  * Copyright (C) 1991, 1992 Thomas G. Lane.
  18.  * This file is part of the Independent JPEG Group's software.
  19.  *
  20.  * For conditions of distribution and use, see the accompanying README file.
  21.  */
  22.  
  23. #include    "jpeg.h"
  24. #include    <stdlib.h>
  25. #include    <macros.h>
  26.  
  27.  
  28. STATIC JSAMPLE **
  29. alloc_sarray(
  30.     long samplesperrow,
  31.     long numrows
  32.     )
  33. {
  34.     JSAMPLE    **result;
  35.     JSAMPLE    *workspace;
  36.     int        nbytes;
  37.     int        i;
  38.  
  39.     nbytes    = numrows * (sizeof(JSAMPLE *) + samplesperrow*sizeof(JSAMPLE));
  40.     result    = (JSAMPLE **) malloc(nbytes);
  41.     if (result != NULL)
  42.     {
  43.     workspace    = (JSAMPLE *) &result[numrows];
  44.  
  45.     for (i = 0; i < numrows; i++)
  46.     {
  47.         result[i] = workspace;
  48.         workspace += samplesperrow;
  49.     }
  50.     }
  51.     return result;
  52. }
  53.  
  54. unsigned char    range_tab[256 * 3];
  55. unsigned char    *range_limit_0_255    = &range_tab[256];
  56.  
  57. static void
  58. init_range_limit()
  59. {
  60.     int        i;
  61.     unsigned char    *p;
  62.  
  63.     p    = range_tab;
  64.  
  65.     for (i = 0; i < 256; i++)
  66.     *p++    = 0;
  67.  
  68.     range_limit_0_255 = p;
  69.  
  70.     for (i = 0; i < 256; i++)
  71.     *p++    = i;
  72.  
  73.     for (i = 0; i < 256; i++)
  74.     *p++    = 255;
  75. }
  76.  
  77. void h2v1_upsample();
  78. /*
  79.  * Un-subsample pixel values of a single component.
  80.  * This version only handles integral sampling ratios.
  81.  */
  82. void
  83. jpegUnSubSample(
  84.     long        input_cols,
  85.     int            input_rows,
  86.     long        output_cols,
  87.     int            output_rows,
  88.     JSAMPLE        **input_data,
  89.     JSAMPLE        **output_data
  90.     )
  91. {
  92.     register    unsigned char    *inptr;
  93.     register    unsigned char    *outptr;
  94.     register    int        i;
  95.         int        inrow, outrow;
  96.         int        h_expand, v_expand;
  97.  
  98.     h_expand    = output_cols / input_cols;
  99.     v_expand    = output_rows / input_rows;
  100.  
  101.     outrow    = 0;
  102.     for (inrow = 0; inrow < input_rows; inrow++)
  103.     {
  104.     inptr    = input_data[inrow];
  105.     outptr    = output_data[outrow];
  106.  
  107.     switch (h_expand)
  108.     {
  109. #if defined(ASM)
  110.         case 1:
  111.         upsample_x1(outptr, inptr, input_cols);
  112.         break;
  113.         case 2:
  114.         upsample_x2(outptr, inptr, input_cols);
  115.         break;
  116. #else
  117.         case 1:
  118.         memcpy(outptr, inptr, input_cols);
  119.         break;
  120.         case 2:
  121.         for (i = 0; i < input_cols; i++, inptr++, outptr += 2)
  122.             outptr[0] = outptr[1] = inptr[0];
  123.         break;
  124. #endif
  125.         case 3:
  126.         for (i = 0; i < input_cols; i++, inptr++, outptr += 3)
  127.             outptr[0] = outptr[1] = outptr[2] = inptr[0];
  128.         break;
  129.         case 4:
  130.         for (i = 0; i < input_cols; i++, inptr++, outptr += 4)
  131.             outptr[0] = outptr[1] = outptr[2] = outptr[3] = inptr[0];
  132.         break;
  133.     }
  134.  
  135.     for (i = 1; i < v_expand; i++)
  136.         memcpy(output_data[outrow+i], output_data[outrow], output_cols);
  137.     outrow += i;
  138.     }
  139. }
  140.  
  141. int
  142. jpegScanDecode(
  143.     FILE        *fp,
  144.     decompress_info_ptr    cinfo,
  145.     JPEG_TABLES        *tables,
  146.     int            restart_interval,
  147.     int            (*put_pixels)()
  148.     )
  149. {
  150.     JBLOCK    MCU_coeff_data[MAX_BLOCKS_IN_MCU];
  151.     JBLOCK    *coeff_data;
  152.     DBLOCK_INFO    MCU_info[MAX_BLOCKS_IN_MCU];
  153.     DBLOCK_INFO    *mcu_info;
  154.     JCOEF    dc_prediction[MAX_COMPS_IN_SCAN];
  155.  
  156.     jpeg_component_info    *compptr;
  157.     int        i, n;
  158.     int        mcu_rows_in_scan;
  159.     int        mcu_row;
  160.     int        mcus_per_row;
  161.     int        mcu;
  162.  
  163.     int        max_h_samp_factor;
  164.     int        max_v_samp_factor;
  165.     int        mcu_width;    /* width (in samples) of an MCU        */
  166.     int        mcu_height;    /* height (in samples) of an MCU    */
  167.     int        r;
  168.  
  169.     int        warn    = 0;
  170.  
  171.     int        restarts_to_go;
  172.     int        next_restart_marker;
  173.  
  174.     JSAMPLE    **subsampled_image[MAX_COMPS_IN_SCAN];
  175.     JSAMPLE    **fullsize_image[MAX_COMPS_IN_SCAN];
  176.  
  177.   int rows_in_mem;        /* # of sample rows in full-size buffers */
  178.   long fullsize_width;        /* # of samples per row in full-size buffers */
  179.   int    mcu_blks;
  180.   long pixel_rows_output;    /* # of pixel rows actually emitted */
  181.  
  182.     r    = 0;
  183.  
  184.     init_range_limit();
  185.  
  186.     if (cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  187.     return jpegError("Too many components for interleaved scan");
  188.  
  189.     /*
  190.      * calculate maximum horizontal and vertical sampling factors
  191.      */
  192.     max_h_samp_factor = 1;
  193.     max_v_samp_factor = 1;
  194.  
  195.     for (i = 0; i < cinfo->num_components; i++)
  196.     {
  197.     int    h_samp_factor;
  198.     int    v_samp_factor;
  199.  
  200.     h_samp_factor = cinfo->comp_info[i].h_samp_factor;
  201.     v_samp_factor = cinfo->comp_info[i].v_samp_factor;
  202.  
  203.     if (h_samp_factor <= 0 || h_samp_factor > MAX_SAMP_FACTOR)
  204.         return jpegError("bad horizontal sampling factor");
  205.     if (v_samp_factor <= 0 || v_samp_factor > MAX_SAMP_FACTOR)
  206.         return jpegError("bad vertical sampling factor");
  207.  
  208.     if (h_samp_factor > max_h_samp_factor)
  209.         max_h_samp_factor = h_samp_factor;
  210.     if (v_samp_factor > max_v_samp_factor)
  211.         max_v_samp_factor = v_samp_factor;
  212.     }
  213.  
  214.     mcu_width    = max_h_samp_factor * DCTSIZE;
  215.     mcu_height    = max_v_samp_factor * DCTSIZE;
  216.  
  217.     mcus_per_row    = (cinfo->image_width + mcu_width - 1) / mcu_width;
  218.     mcu_rows_in_scan    = (cinfo->image_height + mcu_height - 1) / mcu_height;
  219.   
  220.     /*
  221.      * Compute dimensions of full-size pixel buffers
  222.      */
  223.     rows_in_mem        = mcu_height;
  224.     fullsize_width    = mcus_per_row * mcu_width;
  225.  
  226.     mcu_blks    = 0;
  227.     mcu_info    = &MCU_info[0];
  228.     for (i = 0; i < cinfo->comps_in_scan; i++)
  229.     {
  230.     int    j, k;
  231.     int    subsampled_width;
  232.  
  233.     compptr = cinfo->cur_comp_info[i];
  234.     subsampled_width = mcus_per_row * compptr->h_samp_factor*DCTSIZE;
  235.  
  236.     for (j = 0; j < compptr->v_samp_factor; j++)
  237.         for (k = 0; k < compptr->h_samp_factor; k++)
  238.         {
  239.         if (mcu_blks >= MAX_BLOCKS_IN_MCU)
  240.             return jpegError("Sampling factors too large for interleaved scan");
  241.         mcu_info->coeff_data    = &MCU_coeff_data[mcu_blks];
  242.         mcu_info->dc_table    = &tables->dc_huff_table[compptr->dc_tbl_no];
  243.         mcu_info->ac_table    = &tables->ac_huff_table[compptr->ac_tbl_no];
  244.         mcu_info->dc_prediction    = &dc_prediction[compptr->component_index];
  245.         mcu_info->quant_table    = &tables->quant_table[compptr->quant_tbl_no];
  246.         ++mcu_info;
  247.         ++mcu_blks;
  248.         }
  249.  
  250.     /*
  251.      * Allocate working memory:
  252.      *    subsampled_image is sample data before unsubsampling
  253.      *    fullsize_image is sample data after unsubsampling 
  254.      */
  255.     fullsize_image[i]    = alloc_sarray(fullsize_width, rows_in_mem);
  256.     subsampled_image[i]    = alloc_sarray(subsampled_width,
  257.                     compptr->v_samp_factor * DCTSIZE);
  258.     }
  259.  
  260.     /*
  261.      * Initialize to read scan data
  262.      */
  263.     huff_decoder_init(fp);
  264.  
  265.     for (i = 0; i < MAX_COMPS_IN_SCAN; i++)
  266.     dc_prediction[i] = 0;
  267.  
  268.     restarts_to_go    = restart_interval;
  269.     next_restart_marker    = 0;
  270.  
  271.     /*
  272.      * Loop over scan's data: rows_in_mem pixel rows are processed per loop
  273.      */
  274.     pixel_rows_output = 0;
  275.  
  276.     for (mcu_row = 0; mcu_row < mcu_rows_in_scan; mcu_row++)
  277.     {
  278.     for (mcu = 0; mcu < mcus_per_row; mcu++)
  279.     {
  280.         if (restart_interval)
  281.         {
  282.         if (restarts_to_go == 0)
  283.         {
  284.             jpegCheckRestart(fp, next_restart_marker);
  285.             restarts_to_go    = restart_interval;
  286.             next_restart_marker    = (next_restart_marker + 1) & 7;
  287.  
  288.             for (i = 0; i < MAX_COMPS_IN_SCAN; i++)
  289.             dc_prediction[i] = 0;
  290.         }
  291.         restarts_to_go--;
  292.         }
  293.  
  294.         memset((void *) MCU_coeff_data, 0, mcu_blks * sizeof(JBLOCK));
  295.  
  296.         warn = jpegDecodeMCU(fp, mcu_blks, MCU_info);
  297.  
  298.         coeff_data = MCU_coeff_data;
  299.         for (i = 0; i < cinfo->comps_in_scan; i++)
  300.         {
  301.         int    j, k;
  302.         compptr = cinfo->cur_comp_info[i];
  303.  
  304.         for (j = 0; j < compptr->v_samp_factor; j++)
  305.         {
  306.             JSAMPLE    **srowptr = subsampled_image[i] + (j * DCTSIZE);
  307.             for (k = 0; k < compptr->h_samp_factor; k++)
  308.             {
  309.             register JSAMPLE    *elemptr;
  310.             register DCTELEM    *blkptr = (DCTELEM *)coeff_data++;
  311.             register JSAMPLE    *range_limit =
  312.                         range_limit_0_255 + CENTERJSAMPLE;
  313.             register int elemr;
  314.  
  315.             jpegReverseDCT(blkptr);    /* perform inverse DCT */
  316.  
  317.             for (elemr = 0; elemr < DCTSIZE; elemr++)
  318.             {
  319.                 elemptr    = srowptr[elemr] +
  320.                     (mcu * compptr->h_samp_factor + k) * DCTSIZE;
  321.                 elemptr[0]    = range_limit[blkptr[0]];
  322.                 elemptr[1]    = range_limit[blkptr[1]];
  323.                 elemptr[2]    = range_limit[blkptr[2]];
  324.                 elemptr[3]    = range_limit[blkptr[3]];
  325.                 elemptr[4]    = range_limit[blkptr[4]];
  326.                 elemptr[5]    = range_limit[blkptr[5]];
  327.                 elemptr[6]    = range_limit[blkptr[6]];
  328.                 elemptr[7]    = range_limit[blkptr[7]];
  329.                 blkptr    += 8;
  330.             }
  331.             }
  332.         }
  333.         }
  334.     }
  335.  
  336.  
  337.     for (i = 0; i < cinfo->comps_in_scan; i++)
  338.     {
  339.         int        j;
  340.         int        v_samp_factor;
  341.         int        subsampled_width;
  342.  
  343.         compptr        = cinfo->cur_comp_info[i];
  344.         v_samp_factor    = compptr->v_samp_factor;
  345.         subsampled_width    = mcus_per_row * compptr->h_samp_factor*DCTSIZE;
  346.  
  347.  
  348.         for (j = 0; j < DCTSIZE; j++)
  349.         jpegUnSubSample(subsampled_width, v_samp_factor,
  350.             fullsize_width, max_v_samp_factor,
  351.             subsampled_image[i] + j * v_samp_factor,
  352.             fullsize_image[i] + j * max_v_samp_factor);
  353.     }
  354.  
  355.     n = cinfo->image_height - pixel_rows_output;
  356.     if (n > rows_in_mem)
  357.         n = rows_in_mem;
  358.     if ((r = put_pixels(0, pixel_rows_output, cinfo->image_width,
  359.         n, fullsize_image)) != 0)
  360.         goto out;
  361.  
  362.     if (warn)
  363.     {
  364.         r = imageWarning("corrupt data in JPEG file");
  365.         goto out;
  366.     }
  367.  
  368.     pixel_rows_output += n;
  369.  
  370.     } /* end of outer loop */
  371.  
  372.     huff_decoder_term(fp);
  373.  
  374. out:
  375.     /*
  376.      * Release working memory
  377.      */
  378.     for (i = 0; i < cinfo->comps_in_scan; i++)
  379.     {
  380.     free((void *)fullsize_image[i]);
  381.     free((void *)subsampled_image[i]);
  382.     }
  383.  
  384.     return r;
  385. }
  386.